package com.openbravo.pos.inventory;

import com.openbravo.format.Formats;
import com.openbravo.pos.forms.AppLocal;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

public class JInventoryLines extends JPanel
{
  private InventoryTableModel m_inventorylines;
  private JScrollPane jScrollPane1;
  private JTable m_tableinventory;

  public JInventoryLines()
  {
    initComponents();
    DefaultTableColumnModel localDefaultTableColumnModel = new DefaultTableColumnModel();
    TableColumn localTableColumn = new TableColumn(0, 200, new DataCellRenderer(2), new DefaultCellEditor(new JTextField()));
    localTableColumn.setHeaderValue(AppLocal.getIntString("label.item"));
    localDefaultTableColumnModel.addColumn(localTableColumn);
    localTableColumn = new TableColumn(1, 75, new DataCellRenderer(4), new DefaultCellEditor(new JTextField()));
    localTableColumn.setHeaderValue(AppLocal.getIntString("label.units"));
    localDefaultTableColumnModel.addColumn(localTableColumn);
    localTableColumn = new TableColumn(2, 75, new DataCellRenderer(4), new DefaultCellEditor(new JTextField()));
    localTableColumn.setHeaderValue(AppLocal.getIntString("label.price"));
    localDefaultTableColumnModel.addColumn(localTableColumn);
    this.m_tableinventory.setColumnModel(localDefaultTableColumnModel);
    this.m_tableinventory.getTableHeader().setReorderingAllowed(false);
    this.m_tableinventory.setRowHeight(40);
    this.m_tableinventory.getSelectionModel().setSelectionMode(0);
    this.m_tableinventory.setIntercellSpacing(new Dimension(0, 1));
    this.m_inventorylines = new InventoryTableModel( );
    this.m_tableinventory.setModel(this.m_inventorylines);
  }

  public void clear()
  {
    this.m_inventorylines.clear();
  }

  public void addLine(InventoryLine paramInventoryLine)
  {
    this.m_inventorylines.addRow(paramInventoryLine);
    setSelectedIndex(this.m_inventorylines.getRowCount() - 1);
  }

  public void deleteLine(int paramInt)
  {
    this.m_inventorylines.removeRow(paramInt);
    if (paramInt >= this.m_inventorylines.getRowCount())
      paramInt = this.m_inventorylines.getRowCount() - 1;
    if ((paramInt >= 0) && (paramInt < this.m_inventorylines.getRowCount()))
      setSelectedIndex(paramInt);
  }

  public void setLine(int paramInt, InventoryLine paramInventoryLine)
  {
    this.m_inventorylines.setRow(paramInt, paramInventoryLine);
    setSelectedIndex(paramInt);
  }

  public InventoryLine getLine(int paramInt)
  {
    return this.m_inventorylines.getRow(paramInt);
  }

  public List<InventoryLine> getLines()
  {
    return this.m_inventorylines.getLines();
  }

  public int getCount()
  {
    return this.m_inventorylines.getRowCount();
  }

  public int getSelectedRow()
  {
    return this.m_tableinventory.getSelectedRow();
  }

  public void setSelectedIndex(int paramInt)
  {
    this.m_tableinventory.getSelectionModel().setSelectionInterval(paramInt, paramInt);
    Rectangle localRectangle = this.m_tableinventory.getCellRect(paramInt, 0, true);
    this.m_tableinventory.scrollRectToVisible(localRectangle);
  }

  public void goDown()
  {
    int i = this.m_tableinventory.getSelectionModel().getMaxSelectionIndex();
    if (i < 0)
    {
      i = 0;
    }
    else
    {
      i++;
      if (i >= this.m_inventorylines.getRowCount())
        i = this.m_inventorylines.getRowCount() - 1;
    }
    if ((i >= 0) && (i < this.m_inventorylines.getRowCount()))
      setSelectedIndex(i);
  }

  public void goUp()
  {
    int i = this.m_tableinventory.getSelectionModel().getMinSelectionIndex();
    if (i < 0)
    {
      i = this.m_inventorylines.getRowCount() - 1;
    }
    else
    {
      i--;
      if (i < 0)
        i = 0;
    }
    if ((i >= 0) && (i < this.m_inventorylines.getRowCount()))
      setSelectedIndex(i);
  }

  private void initComponents()
  {
    this.jScrollPane1 = new JScrollPane();
    this.m_tableinventory = new JTable();
    setLayout(new BorderLayout());
    this.m_tableinventory.setAutoCreateColumnsFromModel(false);
    this.m_tableinventory.setAutoResizeMode(0);
    this.m_tableinventory.setFocusable(false);
    this.m_tableinventory.setRequestFocusEnabled(false);
    this.m_tableinventory.setShowVerticalLines(false);
    this.jScrollPane1.setViewportView(this.m_tableinventory);
    add(this.jScrollPane1, "Center");
  }

  private static class DataCellRenderer extends DefaultTableCellRenderer
  {
    private int m_iAlignment;

    public DataCellRenderer(int paramInt)
    {
      this.m_iAlignment = paramInt;
    }

    public Component getTableCellRendererComponent(JTable paramJTable, Object paramObject, boolean paramBoolean1, boolean paramBoolean2, int paramInt1, int paramInt2)
    {
      JLabel localJLabel = (JLabel)super.getTableCellRendererComponent(paramJTable, paramObject, paramBoolean1, paramBoolean2, paramInt1, paramInt2);
      localJLabel.setVerticalAlignment(1);
      localJLabel.setHorizontalAlignment(this.m_iAlignment);
      if (!paramBoolean1)
        localJLabel.setBackground(UIManager.getDefaults().getColor("TextField.disabledBackground"));
      return localJLabel;
    }
  }

  private static class InventoryTableModel extends AbstractTableModel
  {
    private ArrayList<InventoryLine> m_rows = new ArrayList();

    public int getRowCount()
    {
      return this.m_rows.size();
    }

    public int getColumnCount()
    {
      return 3;
    }

    public String getColumnName(int paramInt)
    {
      return "a";
    }

    public Object getValueAt(int paramInt1, int paramInt2)
    {
      InventoryLine localInventoryLine = (InventoryLine)this.m_rows.get(paramInt1);
      switch (paramInt2)
      {
      case 0:
        return new StringBuilder().append("<html>").append(localInventoryLine.getProductName()).append(localInventoryLine.getProductAttSetInstDesc() == null ? "" : new StringBuilder().append("<br>").append(localInventoryLine.getProductAttSetInstDesc()).toString()).toString();
      case 1:
        return new StringBuilder().append("x").append(Formats.DOUBLE.formatValue(Double.valueOf(localInventoryLine.getMultiply()))).toString();
      case 2:
        return Formats.CURRENCY.formatValue(Double.valueOf(localInventoryLine.getPrice()));
      }
      return null;
    }

    public boolean isCellEditable(int paramInt1, int paramInt2)
    {
      return false;
    }

    public void clear()
    {
      int i = getRowCount();
      if (i > 0)
      {
        this.m_rows.clear();
        fireTableRowsDeleted(0, i - 1);
      }
    }

    public List<InventoryLine> getLines()
    {
      return this.m_rows;
    }

    public InventoryLine getRow(int paramInt)
    {
      return (InventoryLine)this.m_rows.get(paramInt);
    }

    public void setRow(int paramInt, InventoryLine paramInventoryLine)
    {
      this.m_rows.set(paramInt, paramInventoryLine);
      fireTableRowsUpdated(paramInt, paramInt);
    }

    public void addRow(InventoryLine paramInventoryLine)
    {
      insertRow(this.m_rows.size(), paramInventoryLine);
    }

    public void insertRow(int paramInt, InventoryLine paramInventoryLine)
    {
      this.m_rows.add(paramInt, paramInventoryLine);
      fireTableRowsInserted(paramInt, paramInt);
    }

    public void removeRow(int paramInt)
    {
      this.m_rows.remove(paramInt);
      fireTableRowsDeleted(paramInt, paramInt);
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.inventory.JInventoryLines
 * JD-Core Version:    0.6.0
 */